Page({
  data: {
    height: '',
    weight: '',
    bmi: null,
    status: '',
    suggestion: '',
    indicatorLeft: '0%',
    canCalculate: false
  },

  // 输入身高时的处理
  onHeightInput(e) {
    const height = e.detail.value;
    this.setData({
      height,
      canCalculate: this.data.weight && height
    });
  },

  // 输入体重时的处理
  onWeightInput(e) {
    const weight = e.detail.value;
    this.setData({
      weight,
      canCalculate: this.data.height && weight
    });
  },

  // 计算 BMI
  calculateBMI() {
    const height = parseFloat(this.data.height) / 100; // 转换为米
    const weight = parseFloat(this.data.weight);
    
    if (!height || !weight) {
      wx.showToast({
        title: '请输入有效的身高和体重',
        icon: 'none'
      });
      return;
    }

    const bmi = (weight / (height * height)).toFixed(1);
    let status = '';
    let suggestion = '';
    let indicatorLeft = '0%';

    // 根据 BMI 值确定状态和建议
    if (bmi < 18.5) {
      status = '偏瘦';
      suggestion = '建议适当增加饮食量，注意营养均衡，可以进行力量训练来增加肌肉量。';
      indicatorLeft = '12.5%';
    } else if (bmi >= 18.5 && bmi < 24) {
      status = '正常';
      suggestion = '恭喜！请继续保持健康的生活方式，定期运动和均衡饮食。';
      indicatorLeft = '37.5%';
    } else if (bmi >= 24 && bmi < 28) {
      status = '超重';
      suggestion = '建议适当控制饮食，增加有氧运动，保持规律作息。';
      indicatorLeft = '62.5%';
    } else {
      status = '肥胖';
      suggestion = '建议在医生指导下制定减重计划，控制饮食，增加运动量，定期体检。';
      indicatorLeft = '87.5%';
    }

    this.setData({
      bmi,
      status,
      suggestion,
      indicatorLeft
    });

    // 震动反馈
    wx.vibrateShort({
      type: 'medium'
    });
  },

  // 重置数据
  resetData() {
    this.setData({
      height: '',
      weight: '',
      bmi: null,
      status: '',
      suggestion: '',
      indicatorLeft: '0%',
      canCalculate: false
    });
  }
}); 